from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime

# 新闻创建模式
class NewsCreate(BaseModel):
    title: str = Field(..., min_length=1, max_length=200, description="新闻标题")
    content: str = Field(..., min_length=1, description="新闻内容")
    image: Optional[str] = Field(None, description="新闻图片URL")

# 新闻更新模式
class NewsUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=200)
    content: Optional[str] = Field(None, min_length=1)
    image: Optional[str] = None

# 新闻响应模式
class NewsResponse(BaseModel):
    id: int
    title: str
    content: str
    image: Optional[str]
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True
        # 配置时间序列化格式为易读格式
        json_encoders = {
            datetime: lambda v: v.strftime("%Y-%m-%d %H:%M:%S") if v else None
        }

# 新闻列表响应
class NewsListResponse(BaseModel):
    items: List[NewsResponse]
    total: int
    page: int
    size: int
    pages: int